<template>
  <view class="ai">
    <go-back></go-back>
    <view class="chat-container">
      <scroll-view
        scroll-y
        class="chat-list"
        :scroll-top="scrollTop"
        @scrolltoupper="loadMoreHistory"
      >
        <view
          class="chat-item"
          v-for="(item, index) in chatList"
          :key="index"
          :class="{ user: item.type === 'user' }"
        >
          <view
            class="avatar"
            :style="{
              backgroundColor: item.type === 'user' ? '#f2f2f4' : '#ffffff',
            }"
          >
            <image src="/static/ai/user.png" mode="aspectFill"> </image>
          </view>
          <view class="content">
            <text>{{ item.content }}</text>
          </view>
        </view>
        <view v-if="loading" class="loading">Thinking...</view>
      </scroll-view>
    </view>

    <view class="input-area">
      <input
        type="text"
        v-model="inputText"
        placeholder="Please enter text"
        @confirm="sendMessage"
      />
      <image
        src="/static/ai/send.png"
        mode="aspectFill"
        class="send-btn"
        @tap="sendMessage"
      ></image>
    </view>
  </view>
</template>

<script>
import ProductCard from "@/components/ProductCard.vue";
import GoBack from "@/components/GoBack.vue";
export default {
  components: {
    ProductCard,
    GoBack,
  },
  data() {
    return {
      chatList: [],
      inputText: "",
      scrollTop: 0,
      loading: false,
      currentProduct: null,
    };
  },
  methods: {
    sendMessage() {
      if (!this.inputText.trim()) return;
      this.scrollToBottom();
      // 添加用户消息
      this.chatList.push({
        type: "user",
        content: this.inputText,
      });
      // this.loading = true
      // 调用AI接口
      this.chatList.push({
        type: "ai",
        content: "好的，请稍后...",
      });
      this.inputText = "";
    },

    scrollToBottom() {
      setTimeout(() => {
        const query = uni.createSelectorQuery().in(this);
        query
          .selectAll(".chat-item")
          .boundingClientRect((data) => {
            if (data && data.length > 0) {
              const lastItem = data[data.length - 1];
              this.scrollTop = lastItem.top + lastItem.height;
            }
          })
          .exec();
      }, 100);
    },
    loadMoreHistory() {
      // 这里可以加载更多历史消息
    },
  },
  onLoad(options) {
    if (options.product) {
      try {
        this.currentProduct = JSON.parse(decodeURIComponent(options.product));
        // 添加商品信息到聊天记录
        this.chatList.push({
          type: "ai",
          content: `您好，我是客服人员，对于${this.currentProduct.productName}，您有什么想了解的吗？`,
        });
      } catch (e) {
        console.error("解析商品信息失败：", e);
      }
    }
  },
};
</script>

<style lang="scss">
.ai {
  display: flex;
  flex-direction: column;
  height: 100vh;
  box-sizing: border-box;
  background: linear-gradient(180deg, #fff8e1 0%, #ffffff 100%);
  /* #ifdef MP-WEIXIN */
  // height: 100vh;
  padding-top: calc(var(--status-bar-height) + 100rpx);
  padding-bottom: calc(var(--status-bar-height));
  /* #endif */

  .chat-container {
    flex: 1;
    overflow: hidden;
    padding: 32rpx 0;

    .chat-list {
      width: 100%;
      height: 100%;
      padding: 0 20rpx;
      box-sizing: border-box;

      .chat-item {
        display: flex;
        margin-bottom: 30rpx;

        &.user {
          flex-direction: row-reverse;

          .content {
            background-color: #ffe60f;
            // color: #fff;
            margin-right: 20rpx;
            margin-left: 0;
          }
        }

        .avatar {
          width: 60rpx;
          height: 60rpx;
          border-radius: 16rpx;
          background-color: #ffffff;
          display: flex;
          justify-content: center;
          align-items: center;
          overflow: hidden;
          box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);

          image {
            width: 48rpx;
            height: 48rpx;
          }
        }

        .content {
          max-width: 100%;
          background-color: #f2f2f4;
          padding: 20rpx;
          border-radius: 10rpx;
          margin-left: 20rpx;
          font-size: 28rpx;
          line-height: 1.5;

          .product-grid {
            column-count: 2;
            column-gap: 20rpx;
          }
        }
      }
    }
  }

  .input-area {
    display: flex;
    padding: 10rpx 20rpx;
    background-color: #ffffff;
    border-radius: 20rpx;
    margin: 0 20rpx 20rpx 20rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);

    input {
      flex: 1;
      height: 60rpx;
      background-color: #ffffff;
      border-radius: 40rpx;
      padding: 0 20rpx 0 0;
      font-size: 28rpx;
    }

    .send-btn {
      width: 48rpx;
      height: 48rpx;
      margin-top: 8rpx;
    }
  }

  .loading {
    text-align: center;
    padding: 20rpx;
    color: #999;
    font-size: 24rpx;
  }
}
</style>
